package jk.zmn.server.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import javax.print.DocFlavor.STRING;

import jk.zmn.server.utils.CODE;
import jk.zmn.server.utils.CloseUtils;

/**
 * @author zmn
 *server服务器
 *	启动方法，
 *	停止
 *	接收客户端
 *
 */
public class Server {
	
	//定义响应中的换行
	private static final String CRLF="\r\n";
	//定义响应中的空格
	private static final String BLANK=" ";
	
	private ServerSocket serverSocket;
	//默认服务器开启
	private boolean isShutting=false;
	
	public static void main(String[] args) {
		Server server = new Server();
		server.start();
	}

	/**
	 * 服务器启动方法
	 * @throws IOException 
	 */
	public void start(){
		try {
			serverSocket = new ServerSocket(8888);
			this.recive();
		} catch (Exception e) {
			e.printStackTrace();
			//关闭server
			isShutting = true;
		}
	}
	
	/**
	 * 服务器接收客户端方法
	 */
	public void recive() {
		try {
			while (!isShutting) {
				new Thread(new Dispatcher(serverSocket.accept())).start();
			}
		} catch (IOException e) {
			isShutting = true;
		}
	}
	/**
	 * 关闭服务器方法
	 */
	public void stop(){
		isShutting = true;
		CloseUtils.closeSocket(serverSocket);
	}
}
